package com.telecom.service.Impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dahuatech.hutool.http.Method;
import com.dahuatech.hutool.json.JSONUtil;
import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.oauth.http.DefaultClient;
import com.dahuatech.icc.oauth.http.IClient;
import com.dahuatech.icc.oauth.model.v202010.GeneralRequest;
import com.dahuatech.icc.oauth.model.v202010.GeneralResponse;
import com.telecom.config.dto.InOutReCordQueryParam;
import com.telecom.domain.AcInOutRecord;
import com.telecom.service.AccessControlService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class AccessControlServiceImpl implements AccessControlService {


    //查询门禁数据
    @Override
    public List<AcInOutRecord> inOutRecordSearch(InOutReCordQueryParam inOutReCordQueryParam) throws ClientException {

        List<AcInOutRecord> acInOutRecordList = null;
        
        IClient iClient = new DefaultClient();
        GeneralRequest generalRequest = new GeneralRequest("/evo-apigw/evo-accesscontrol/1.0.0/card/accessControl" +
                "/swingCardRecord/bycondition/combined",
                Method.POST);
        Map<String,Object> map = new HashMap<>();
        map.put("cardNumber","12352334");
        map.put("channelCode","ACC_1001682$7$0$1");
        map.put("deptIds","10,11");
//        map.put("endSwingTime",inOutReCordQueryParam.getEndTime());
        map.put("endSwingTime","2020-12-31 23:59:59");
        map.put("enterOrExit","1");
        map.put("openResult",1);
        map.put("openType","51");
        map.put("pageNum",1);
        map.put("pageSize","20");
        map.put("personCode","55542");
//        map.put("personName",inOutReCordQueryParam.getName());
        map.put("personName","张三");
//        map.put("startSwingTime",inOutReCordQueryParam.getStartTime());
        map.put("startSwingTime","2020-02-26 00:00:00");
        map.put("internalCall",false);

        generalRequest.body(JSONUtil.toJsonStr(map));

        GeneralResponse generalResponse =iClient.doAction(generalRequest, generalRequest.getResponseClass());
        String result = generalResponse.getResult();
        Map<String,Object> hashMap = JSONObject.parseObject(result, HashMap.class);
        Map<String,Object> data=(Map<String,Object>) hashMap.get("data");
        List<Map<String,Object>> list =(List<Map<String,Object>>) data.get("pageData");
        
        if(list != null && list.size() > 0){
            acInOutRecordList = new ArrayList<>();
            for (Map<String, Object> stringObjectMap : list) {
                AcInOutRecord acInOutRecord = new AcInOutRecord();
                acInOutRecord.setName(stringObjectMap.get("personName").toString());
                acInOutRecord.setCardNo(stringObjectMap.get("paperNumber").toString());
                acInOutRecord.setRecordTime(stringObjectMap.get("swingTime").toString());
                acInOutRecord.setPersonnelType(stringObjectMap.get("imageType").toString());
                acInOutRecord.setAccessControlName(stringObjectMap.get("deviceName").toString());
            }
        }
        return acInOutRecordList;
    }
}
